﻿using System;
using System.Data;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using webapp4U;
using webapp4U.BOL;

namespace webapp4U.UI
{
    public partial class ItemDetail : BasePage
    {
        private string ItemKey = "";
        public string ImageName = "";
        private string InClassKey = "";
        string error = "";
        protected void Page_Load(object sender, EventArgs e)
        {

            ItemKey = Globals.GetStringFromQueryString("ItemKey");

            webapp4U.BOL.Products obj = webapp4U.BOL.Products.GetProductsByItemKey(ItemKey, ref error);
            InClassKey = obj.InClassKey;
            lblProductCode.Text = obj.ItemKey.Trim();
            lblProductName.Text = obj.Description;
            txtItemkey.Value = obj.ItemKey;
            if (obj.UnitPrice <= 0)
            {
                lblPrice.Text = ResourceManager.GetString("Product_Price_Lienhe");
                PanelAddToCart.Visible = false;
            }
            else
            {
                lblPrice.Text = obj.UnitPrice.ToString() + " VNĐ";
                string CallBack = Page.ClientScript.GetCallbackEventReference(Page.Master.FindControl("ShoppingCart1"), "document.getElementById('" + txtItemkey.ClientID + "').value", "RateContent", "null");
                btnAddToCart.Attributes["onclick"] = String.Format("javascript:{0}", CallBack);

            }
            ImageObject1.ImageUrl = "~/Page/Products/Image.ashx?Size=M2&ImageUrl=" + obj.ImageUrl;
            lblDesc.Text = obj.Description;
            ImageName = obj.ImageUrl;
            this.Load_Title(obj.Description);
        }


        private void Load_Title(string ProductName)
        {
            string strTile = "";
            strTile = InClassCategory.GetNameCategoryByItemKey(ItemKey, ref error);
            if (strTile != "")
            {
                strTile = " > " + strTile;
            }

            strTile = strTile + " > " + ProductName;

            Title.TitleName = InClassCategory.GetCommodNameCategoryByItemKey(ItemKey, ref error) + strTile;
        }


        protected void btAddToCart_Click(object sender, EventArgs e)
        {
            Products obj = Products.GetProductsByItemKey(ItemKey, ref error);
            ShoppingCart shop = new ShoppingCart();
            shop.AddedDate = DateTime.Now;
            shop.ItemKey = obj.ItemKey;
            shop.Description = obj.Description;
            shop.UnitPrice = obj.UnitPrice;
            shop.Quantity = 1;
            shop.Subtotal = obj.UnitPrice;

            List<ShoppingCart> ListShopCart = new List<ShoppingCart>();
            if (Session["ShoppingCart"] == null)
            {
                ListShopCart.Add(shop);
                Session["ShoppingCart"] = ListShopCart;
            }
            else
            {
                ListShopCart = (List<ShoppingCart>)Session["ShoppingCart"];
                Session["ShoppingCart"] = ShoppingCart.AddItemToShoppingCart(ListShopCart, shop);
            }

            string backUrl = Globals.ApplicationPath + "Default.aspx?Homepage=";
            if (Session["BackPageUrl"] != null)
            {
                backUrl = Session["BackPageUrl"].ToString();
            }
            Response.Redirect(backUrl);
            //Response.Redirect(Globals.ApplicationPath + "Page/Products/ShoppingCart.aspx?InClassKey="+ InClassKey);

        }
        protected void btnBackPage_Click(object sender, EventArgs e)
        {
            string backUrl = Globals.ApplicationPath + "Default.aspx?Homepage=";
            if (Session["BackPageUrl"] != null)
            {
                backUrl = Session["BackPageUrl"].ToString();
            }

            Response.Redirect(backUrl);
        }

        protected void btn_ProductOrder_Click(object sender, EventArgs e)
        {
            
            //Products obj = Products.GetProductsByItemKey(ItemKey, ref error);
            //ShoppingCart shop = new ShoppingCart();
            //shop.AddedDate = DateTime.Now;
            //shop.ItemKey = obj.ItemKey;
            //shop.Description = obj.Description;
            //shop.UnitPrice = obj.UnitPrice;
            //shop.Quantity = 1;
            //shop.Subtotal = obj.UnitPrice;

            //List<ShoppingCart> ListShopCart = new List<ShoppingCart>();
            //if (Session["ShoppingCart"] == null)
            //{
            //    ListShopCart.Add(shop);
            //    Session["ShoppingCart"] = ListShopCart;
            //}
            //else
            //{
            //    ListShopCart = (List<ShoppingCart>)Session["ShoppingCart"];
            //    Session["ShoppingCart"] = ShoppingCart.AddItemToShoppingCart(ListShopCart, shop);
            //}

            //Response.Redirect(Globals.ApplicationPath + "Page/Products/shoppingcart.aspx?Action=DetailCart");
        }
    }
}